package com.xique.client.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.client.bean.domain.ParkIn;
import com.xique.client.bean.search.ParkInSearch;
import com.xique.client.mapper.ParkInMapper;
import com.xique.client.service.service.IParkInService;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2021/11/8 14:20
 */
@Service
public class ParkInServiceImpl implements IParkInService {

    @Autowired
    private ParkInMapper parkInMapper;

    /**
     * 查询入场记录
     *
     * @param id 入场记录ID
     * @return 入场记录
     */
    @Override
    public ParkIn selectParkInById(Long id)
    {
        return parkInMapper.selectById(id);
    }

    @Override
    public ParkIn selectParkIn(ParkInSearch search) {
        QueryWrapper<ParkIn> wrapper = new QueryWrapper<>();
        if (null != search.getId()) {
            wrapper.eq("id", search.getId());
        }
        if (null != search.getLaneIds()) {
            wrapper.in("in_lane_id", search.getLaneIds());
        }
        if (null != search.getLaneId()) {
            wrapper.eq("in_lane_id", search.getLaneId());
        }
        if (StringUtils.isNotEmpty(search.getParkingSerial())) {
            wrapper.eq("parking_serial", search.getParkingSerial());
        }
        wrapper.orderByDesc("UNIX_TIMESTAMP(update_time)")
                .last("limit 1");

        return parkInMapper.selectOne(wrapper);
    }

    /**
     * 查询入场记录列表
     *
     * @param search 入场记录
     * @return 入场记录
     */
    @Override
    public List<ParkIn> selectParkInList(ParkInSearch search)
    {
        QueryWrapper<ParkIn> wrapper = new QueryWrapper<>();
        if (null != search.getLaneIds()) {
            wrapper.in("in_lane_id", search.getLaneIds());
        }
        if (search.isAsc()) {
            wrapper.orderByAsc(search.getOrderBy());
        } else {
            wrapper.orderByDesc(search.getOrderBy());
        }
        if (null != search.getLimit()) {
            wrapper.last("limit " + search.getLimit());
        }
        return parkInMapper.selectList(wrapper);
    }

    /**
     * 新增入场记录
     *
     * @param parkIn 入场记录
     * @return 结果
     */
    @Override
    public int insertParkIn(ParkIn parkIn)
    {
        parkIn.setCreateTime(DateUtils.getNowDate());
        return parkInMapper.insert(parkIn);
    }

    /**
     * 修改入场记录
     *
     * @param parkIn 入场记录
     * @return 结果
     */
    @Override
    public int updateParkIn(ParkIn parkIn)
    {
        parkIn.setUpdateTime(DateUtils.getNowDate());
        return parkInMapper.updateById(parkIn);
    }

    /**
     * 删除入场记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteParkInByIds(List<Long>  ids)
    {
        return parkInMapper.deleteBatchIds(ids);
    }

    /**
     * 删除入场记录信息
     *
     * @param id 入场记录ID
     * @return 结果
     */
    @Override
    public int deleteParkInById(Long id)
    {
        return parkInMapper.deleteById(id);
    }
}
